/*
 * Created by JFormDesigner on Tue Jul 07 19:18:30 CST 2020
 */
package entity.JPanel;

import utils.JDBCUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.imageio.ImageIO;
import javax.swing.*;

/*
 * @author Brainrain
 */
public class UserLoginJPanel extends JPanel {
    private static BufferedImage bg;
    private static JLabel label1;
    private static JLabel label2;
    private static JLabel label3;
    private static JTextField textField1;
    private static JButton button1;
    private static JButton button2;
    private static JPasswordField passwordField1;
    private static Connection con;
    private static PreparedStatement preparedStatement;
    private ResultSet re;

    public UserLoginJPanel() {
        //initComponents();
        setSize(500, 500);
        try {
            bg = ImageIO.read(UserLoginJPanel.class.getClassLoader().getResourceAsStream("resources/images/H.jpg"));
        } catch (IOException e) {
            System.out.println("NULL");
        }
    }

    /**
     * 在面板上绘制背景图片
     *
     * @param g
     */
    public void paintComponent(Graphics g) {
        g.drawImage(bg, 0, 0, this.getWidth(), this.getHeight(), null);//绘制背景图片
    }

    /**
     * 设置面板的各个组件
     */
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        passwordField1 = new JPasswordField();
        //======== this ========
        setBorder(null);
        setLayout(null);
        add(label1);
        label1.setBounds(new Rectangle(new Point(110, 60), label1.getPreferredSize()));
        //---- label2 ----
        label2.setText("\u8d26\u53f7\uff1a");
        label2.setForeground(Color.blue);
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
        add(label2);
        label2.setBounds(135, 65, 75, 55);
        //---- label3 ----
        label3.setText("\u5bc6\u7801\uff1a");
        label3.setForeground(Color.blue);
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
        add(label3);
        label3.setBounds(135, 140, 75, 50);
        add(textField1);
        textField1.setBounds(195, 75, 155, 35);
        //---- button1 ----
        button1.setText("\u767b\u5f55");
        button1.setBackground(new Color(153, 255, 0));
        button1.setEnabled(true);
        button1.setFont(new Font("\u6977\u4f53", Font.BOLD, 15));
        button1.setBorder(null);
        //按钮失去焦点
        button1.setFocusPainted(false);
        add(button1);
        //登录按钮事件监听
        button1.addActionListener((e) -> {
            try {
                con = JDBCUtil.getConnect();
            } catch (Exception e1) {
            }
            System.out.println(con);
            System.out.println("button1");
            String userinputid = textField1.getText();
            String userinputpassword = String.valueOf(passwordField1.getPassword());
            System.out.println("user:" + userinputid);
            System.out.println("password:" + userinputpassword);
            if (userinputid.equals("") || userinputpassword.equals("")) {
                JOptionPane.showMessageDialog(this, "不能输入空值", "nlll", JOptionPane.WARNING_MESSAGE);
            } else {
                String sql = "select * from users_table where name=?";
                try {
                    preparedStatement = con.prepareStatement(sql);
                    preparedStatement.setString(1, userinputid);
                    re = preparedStatement.executeQuery();
                    String pass = "";
                    while (re.next()) {
                        pass = re.getString("password");
                        System.out.println("密码：" + re.getString("password"));
                    }
                    System.out.println("pass:" + pass);
                    if (re != null) {
                        if (pass.equals(userinputpassword)) {
                            JOptionPane.showMessageDialog(this, "登录成功", "success", JOptionPane.WARNING_MESSAGE);
                            JDBCUtil.close(preparedStatement, re, con);

                        } else {
                            JOptionPane.showMessageDialog(this, "密码错误", "fail", JOptionPane.WARNING_MESSAGE);
                        }
                    } else {
                        JOptionPane.showMessageDialog(this, "不存在该账号", "fail", JOptionPane.WARNING_MESSAGE);
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                } finally {
                    JDBCUtil.close(preparedStatement, re, con);
                }
            }
        });
        button1.setBounds(165, 225, 85, 50);
        //---- button2 ----
        button2.setText("\u6ce8\u518c");
        button2.setBackground(new Color(153, 255, 0));
        button2.setEnabled(true);
        button2.setFont(new Font("\u6977\u4f53", Font.BOLD, 15));
        button2.setBorderPainted(false);
        //按钮失去焦点
        button2.setFocusPainted(false);
        add(button2);
        //注册按钮事件监听
        button2.addActionListener((e) -> {
            System.out.println("button2");
        });
        button2.setBounds(290, 225, 80, 50);
        add(passwordField1);
        passwordField1.setBounds(195, 150, 155, 35);
        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for (int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }

    }
}
